<template>
  <Spin :spinning="loading" tip="Loading...">
    <div>
      <!-- 故障现象维护--车厂 -->
      <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="4" :collapse="collapse" labelWidth="90" @filterChange="changeHandle"></TopFilter>
      <!-- table -->
      <FilterTable
        ref="table"
        :columns="columns"
        :params="fetchParams"
        :fetchapi="fetchapi"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columns = columns)"
        :onRowSelectChange="selectChange"
      >
        <template slot="controls" slot-scope="props">
<!--          <multiuse-button size="small" type="primary" icon="el-icon-check" :click="qiyong.bind()" :disabled="disableds">启用</multiuse-button>-->
<!--          <multiuse-button size="small" type="primary" icon="el-icon-close" :click="tingyong.bind()":disabled="disableds">禁用</multiuse-button>-->
          <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="add()">新增</multiuse-button>
<!--          <multiuse-button type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>-->
<!--          <multiuse-button size="small" type="primary" :click="downloadTemplate.bind()">下载模板</multiuse-button>-->
<!--          <multiuse-button size="small" icon="el-icon-upload" @click="showImportFun()" >导入</multiuse-button>-->
        </template>
      </FilterTable>
      <BaseDialog :visible.sync="visibleAdd" :title="'二级系统新增'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
        <AddForm @close="closeAdd" @save="saveAdd"></AddForm>
      </BaseDialog>
      <BaseDialog :visible.sync="emitVisible" title="二级系统编辑" width="920" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <editForm @close="emitCloseHandle" @save="emitChange" :rowData="rowData"/>
      </BaseDialog>
<!--      <BaseDialog :visible.sync="showImport" :closable="false" icon="el-icon-upload" title="导入" width="800px" destroyOnClose :containerStyle="{ height: 'calc(100% - 120px)', overflow: 'auto', paddingBottom: '60px' }">-->
<!--        <importExcel @close="showImport = false" @importclose="editsave()" url="/api/cms_claimfac/rPFaultAppear/importExcel"/>-->
<!--      </BaseDialog>-->
    </div>
  </Spin>
</template>
<script>
import { getListByPage, newSave, piliangqiyong, piliangtingyong, getListForExport, downloadModel,exporturl } from '@/api/claim/factory/FaultAppearMaintain';
import { exportExcelFile } from '@/utils';
import AddForm from './AddForm.vue';
import editForm from './editForm.vue';
// import importOrderParameterModel from './importOrderParameterModel.vue';
// import importExcel from '@/pages/claimDemage/tools/importExcel.vue';

export default {
  components: {
    AddForm,
    editForm
  },
  data() {
    return {
      disableds: true,
      dataList: [],
      visible: false,
      visibleDrawer: false,
      collapse: true,
      emitVisible: false,
      rowData: {},
      fetchapi: getListByPage,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: {
        level: 2
      }, //ipa 接口
      selectedRows: [], //选中的行
      dialogTitle: '选择车辆',
      visibleAdd: false,
      showImport: false,
      loading:false,
    };
  },
  created() {
    this.getLevelOne();
  },
  methods: {
    //一级下拉框
    async getLevelOne() {
      console.log("===== 获取下拉框数据 ====== ")
      const res = await getListByPage({
        level: 1,
        pageNum: 1,
        pageSize: 99999
      });
      console.log("==========",res.data);
      if (res.resultCode == 200) {
        this.topFilterInit.find(x => x.fieldName == 'parentFaultId').initialValue = '';
        this.topFilterInit.find(x => x.fieldName == 'parentFaultId').itemList = res.data.records.map(y => ({ text: y.sysName1, value: y.faultId }));
      }
    },
    // Excel更新导入
    showImportFun() {
      this.showImport = true;
    },
    async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000115' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
    },
    editsave() {
      this.showImport = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val);
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      this.disableds = this.selectedRows.length <= 0;
      console.log(this.selectedRows);
    },
    // close(){
    //   this.visible = false;
    // },
    // chooseOrderNo() {
    //   this.visible = true;
    // },
    // save(obj) {
    //   this.topFilterInit.find(x => x.fieldName == 'model').initialValue = obj[0].address;
    //   this.visible = false;
    // },
    //批量启用
    async qiyong() {
      if (this.selectedRows.length > 0) {
        let str = '';
        this.selectedRows.map(x => {
          str += x.faultId + ',';
        });
        console.log(str);
        const res = await piliangqiyong({ids: str, status: '1'});
        this.fetchParams = {...this.fetchParams};
      } else {
        return this.$notify.warning({title: '提示信息', message: '请勾选后再进行启用！'});
      }
    },
    //批量停用
    async tingyong() {
      if (this.selectedRows.length > 0) {
        let str = '';
        this.selectedRows.map(x => {
          str += x.faultId + ',';
        });
        console.log(str);
        const res = await piliangtingyong({ids: str, status: '0'});
        this.fetchParams = {...this.fetchParams};
      } else {
        return this.$notify.warning({title: '提示信息', message: '请勾选后再进行禁用！'});
      }
    },
    //下载模板
    downloadTemplate() {
      this.$confirm('此操作将下载excel模板, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.exportLoading = true;
        downloadModel().then(res => {
          console.log(res);
          exportExcelFile(res.data, '故障现象描述导入模板');
          this.exportLoading = false;
        });
      });
    },
    add() {
      this.visibleAdd = true;
    },
    emitFun(data) {
      this.rowData = data;
      this.emitVisible = true;
    },
    emitCloseHandle() {
      this.emitVisible = false;
    },
    emitChange(val) {
      this.emitVisible = false;
      this.fetchParams = { ...this.fetchParams };
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    saveAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'SELECT',
          label: '一级系统名称',
          fieldName: 'parentFaultId',
          placeholder: '请选择一级系统名称...',
          initialValue: '',
          itemList: [],
          change: val => {
            console.log("选择一级系统名称")
          }
        },
        {type: 'INPUT', label: '二级系统代码', fieldName: 'sysCode2', placeholder: '请输入二级系统代码...',},
        {type: 'INPUT', label: '二级系统名称', fieldName: 'sysName2', placeholder: '请输入二级系统名称...',},
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'faStatus',
          initialValue: '',
          itemList: [{text: '是', value: 1}, {text: '否', value: 0}],
          // rules: [
          //   {
          //     required: true,
          //     message: '必填',
          //     trigger: 'change'
          //   }
          // ]
        },
        // { type: 'INPUT', label: '二级', fieldName: 'sysCode2', placeholder: '请输入二级...', },
        // { type: 'INPUT', label: '二级名称', fieldName: 'sysName2', placeholder: '请输入二级名称...', },
        // { type: 'INPUT', label: '三级', fieldName: 'sysCode3', placeholder: '请输入三级...', },
        // { type: 'INPUT', label: '三级名称', fieldName: 'sysName3', placeholder: '请输入三级名称...',  },
        // { type: 'INPUT', label: '故障代码', fieldName: 'sysCode4', placeholder: '请输入故障代码...', },
        // { type: 'INPUT', label: '故障名称', fieldName: 'sysName4', placeholder: '请输入故障名称...', }
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          fixed: 'left',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 80,
          render: (props, h) => {
            return (
              <div style="text-align:center">
                <multiuse-button size="mini" type="text" onClick={() => this.emitFun(props.row)}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        // { title: '状态', dataIndex: 'statusStr', sorter: true },
        { title: '一级系统名称', dataIndex: 'sysName1', sorter: true },
        { title: '二级系统代码', dataIndex: 'sysCode2', sorter: true },
        { title: '二级系统名称', dataIndex: 'sysName2', sorter: true },
        { title: '是否有效', dataIndex: 'statusStr', sorter: true },
        // { title: '二级', dataIndex: 'sysCode2', sorter: true },
        // { title: '二级名称', dataIndex: 'sysName2', sorter: true },
        // { title: '三级', dataIndex: 'sysCode3', sorter: true },
        // { title: '三级名称', dataIndex: 'sysName3', sorter: true },
        // { title: '故障代码', dataIndex: 'sysCode4', sorter: true },
        // { title: '故障名称', dataIndex: 'sysName4', sorter: true },
        // { title: '创建人', dataIndex: 'createdBy', sorter: true },
        // { title: '创建时间', dataIndex: 'createdAt', sorter: true, width: 140 },
        // { title: '修改人', dataIndex: 'updatedBy', sorter: true },
        // { title: '修改时间', dataIndex: 'updatedAt', sorter: true, width: 140 }
      ];
    },
  }
};
</script>

<style lang="less" scoped></style>
